package controller

import (
	"sugar/database"
	"sugar/errorcode"
	"sugar/response"

	"gopkg.in/guregu/null.v3"
)

func (op *Operation) checkModelAuth(userID string, modelID int64, auth int) (bool, error) {
	return true, nil
}

//
func (op *Operation) ListModel(userID string, pageSize int, pageNo int) *response.Response {
	op.BeginOp()
	defer op.EndOp()
	models, err := op.DbOp.ListModelByUser(userID, pageSize, pageNo)
	if err != nil {
		panic(errorcode.ListModelError)
	}
	return response.NewSuccess(models)
}

//
func (op *Operation) GetModel(userID string, modelID int64) *response.Response {
	op.BeginOp()
	defer op.EndOp()
	model, err := op.DbOp.GetModelByID(modelID)
	op.checkModelAuth(userID, modelID, 0)
	if err != nil {
		panic(errorcode.GetModelError)
	}
	return response.NewSuccess(model)
}

//
func (op *Operation) CreateModel(userID string, modelName string, modelClass string, modelData []byte, dependencies null.String, description null.String, modelType int, modelFileType int, modelFileName null.String, public int) *response.Response {
	op.BeginOp()
	defer op.EndOp()
	if exist, _ := op.DbOp.IsModelExist(userID, modelName); exist {
		panic(errorcode.ModelAlreadyExistError)
	}
	modelID, err := op.DbOp.CreateModel(userID, modelName, modelClass, modelData, dependencies, description, modelType, modelFileType, modelFileName, public, null.NewString("", false))
	if err != nil {
		panic(errorcode.CreateModelError)
	}
	res := make(map[string]interface{})
	res["id"] = modelID
	return response.NewSuccess(res)
}

func (op *Operation) checkModelAvailable(userID string, modelID int64) (*database.Model, error) {
	model, err := op.DbOp.GetModelByID(modelID)
	if err != nil {
		panic(errorcode.GetDatasetError)
	}
	op.checkModelAuth(userID, modelID, 0)
	return model, nil
}
